using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using Athena.SBW;

namespace Athena.Plugins
{
    public partial class SliderControl : UserControl
    {
        public SliderControl() : this(null,null) { }


        private SimulationControl _oSimulationControl;

        public SimulationControl SimulationControl
        {
            get { return _oSimulationControl; }
            set { _oSimulationControl = value; }
        }

        public SliderControl(SimulationControl oControl, Simulator oSimulator, Analyzer oAnalyzer, string sId, double dValue)
            : this(oSimulator, oAnalyzer)
        {
            _oSimulationControl = oControl;
            oName.Text = sId;
            _dValue = dValue;
            oValue.Text = dValue.ToString();
        }
        public SliderControl(Simulator oSimulator, Analyzer oAnalyzer)
        {
            //Simulator = oSimulator;
            //Analyzer = oAnalyzer;

            InitializeComponent();

            oSlider.Maximum = 500;
            oSlider.Minimum = -500;
            oSlider.Value = 0;
        }

        /// <summary>
        /// get or set the simulator to use
        /// </summary>
        public Simulator Simulator
        {
            get { return ToolManager.Instance.Simulator; }
            set { ToolManager.Instance.Simulator = value; }
        }

        /// <summary>
        /// get or set the analyzer to use. The Analyzer plots the rate functions and 
        /// the vector fields
        /// </summary>
        public Analyzer Analyzer
        {
            get { return ToolManager.Instance.Analyzer; }
            set { ToolManager.Instance.Analyzer = value; }
        }


        public string Id
        {
            get { return oName.Text; }
            set 
            { 
                oName.Text = value;
                if (Simulator != null && Simulator.HasModel)
                {
                    Value = Simulator.getValue(value);
                    oSlider.Value = oSlider.Maximum / 2;
                }
            }
        }

        private double _dValue;

        public double Value
        {
            get { return _dValue; }
            set 
            { 
                _dValue = value;
                oValue.Text = value.ToString();
                if (Simulator != null && Simulator.HasModel)
                {
                    Simulator.reset();                    
                    Simulator.setValue(Id, value);
                }
                if (Analyzer != null && Analyzer.HasModel)
                {
                    Analyzer.setValue(Id, value);
                }
                if (_oSimulationControl != null)
                {
                    _oSimulationControl.UpdateSimulationResults(false);
                    _oSimulationControl.UpdateRatePlots();
                }
            }
        }

        public override string ToString()
        {
            return Id + ": " + Value.ToString();
        }


        bool _bUpdating = false;

        private void oSlider_Scroll(object sender, EventArgs e)
        {
            if (!_bUpdating)
            {
                _bUpdating = true;
                double dChange = (double)oSlider.Value / 500f;
                Value += dChange;
                _bUpdating = false;
            }
        }

        public void SmallChangeUp()
        {
            if (oSlider.Value < 0) oSlider.Value = 0; 
            oSlider.Value += 5;
            oSlider_Scroll(this, EventArgs.Empty);
        }

        public void SmallChangeDown()
        {
            if (oSlider.Value > 0) oSlider.Value = 0;
            oSlider.Value -= 5;
            oSlider_Scroll(this, EventArgs.Empty);
        }

        private void oValue_TextChanged(object sender, EventArgs e)
        {
            if (!_bUpdating)
            {
                _bUpdating = true;
                try
                {
                    Value = Convert.ToDouble(oValue.Text);
                }
                catch (Exception)
                {

                }
                _bUpdating = false;
            }
        }

    }
}
